package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysRoleMenuMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISysMenuService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 菜单信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/menu")
public class SysMenuController extends BaseController {
    @Autowired
    private ISysMenuService menuService;


    @Autowired
    SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    ISysUserService sysUserService;

    @Autowired
    SysRoleMenuMapper sysRoleMenuMapper;

//    /**
//     * 获取菜单列表
//     */
//    @PreAuthorize("@ss.hasPermi('system:menu:list')")
//    @GetMapping("/list")
//    public AjaxResult list(SysMenu menu)
//    {
//        List<SysMenu> menus = menuService.selectMenuList(menu, getUserId());
//        return AjaxResult.success(menus);
//    }

    @GetMapping("/treeselect")
    public AjaxResult treeselect(SysMenu menu) {
        List<SysMenu> menus = menuService.selectMenuList(menu, getUserId());
        return AjaxResult.success(menuService.buildMenuTreeSelect(menus));
    }

    //checkedKeys    menus
    @GetMapping("/roleMenuTreeselect/{roleId}")
    public AjaxResult getMenuByRoleId(@PathVariable Long roleId) {
        List<Long> longs = menuService.selectMenuListByRoleId(roleId);
        for (Long aLong : longs) {
            System.out.println(aLong);
        }
        List<SysMenu> menus = menuService.selectMenuList(new SysMenu(), getUserId());
        List<TreeSelect> treeSelects = menuService.buildMenuTreeSelect(menus);
        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put("menus", treeSelects);
        ajaxResult.put("checkedKeys", longs);
        return ajaxResult;
    }










    @GetMapping("/list")
    public AjaxResult list(SysMenu menu)
    {
        List<SysMenu> menus = menuService.selectAll(menu,getUserId());
        return AjaxResult.success(menus);
    }
    @DeleteMapping("/{menuId}")
    public int deleteMenuId(@PathVariable(value = "menuId",required = false) Long menuId){
        System.out.println(menuId+"哈哈");
        int cn=menuService.deleteMenuId(menuId);
        return cn;
    }


    @GetMapping("/{menuId}")
    public SysMenu selectMenuId(@PathVariable(value = "menuId",required = false)Long menuId){
        SysMenu sysMenu=menuService.selectMenuId(menuId);
        System.out.println(sysMenu);
        return sysMenu;
    }

    @Transactional
    @PostMapping("/add")
    public int insertMenu(@Validated @RequestBody SysMenu sysMenu){

        SysUser sysUser=sysUserService.selectuserId(getUserId());
        if (sysUser.getUserId()==1){
            sysMenu.setCreateBy(sysUser.getUserName());
            sysMenu.setCreateTime(new Date());
            int cn=menuService.insertIntoMenu(sysMenu);
            return cn;
        }else {
            sysMenu.setCreateBy(sysUser.getUserName());
            sysMenu.setCreateTime(new Date());
            int cn=menuService.insertIntoMenu(sysMenu);
            SysUserRole sysUserRole=sysUserRoleMapper.selectUserId(getUserId());
            SysMenu sysMenu1=menuService.selectDescTime();
            int cnn=sysRoleMenuMapper.insertRoleMenuId(sysUserRole.getRoleId(),sysMenu1.getMenuId());
            return cn;
        }
    }

    @PutMapping("/update")
    public int updateMenu(@Validated @RequestBody SysMenu sysMenu){
        SysUser sysUser=sysUserService.selectuserId(getUserId());
        sysMenu.setUpdateBy(sysUser.getUserName());
        sysMenu.setUpdateTime(new Date());
        int cn=menuService.updateMenU(sysMenu);
        return cn;
    }
}